from starlette.responses import FileResponse

from agent.ananlys_agent import create_analys_agent
from agent.comment_agent import comment_with_agent
from models.chat_schemas import ChatRequest,ChatHistoryResponse
from models.json_response import JsonData
from core.exceptions import ApiException
from typing import Dict,Any
from fastapi import APIRouter, UploadFile
from service.chat_service import ChatService 
from fastapi import Depends
from core.auth import get_currnt_user
from fastapi.responses import StreamingResponse
from agent.chat_agent import generate_stream_response
from service.file_service import FileService
from agent.course_agent import file_embedding
router = APIRouter(
    prefix="/api/teach",
    tags=["教学助手"]
)

@router.post("/chat")
async def chat_stream(request:ChatRequest,
                  chat_service:ChatService=Depends(ChatService),
                    course_id:str=None,
                  account_id:str=None):
    if account_id is None or course_id is None:
        return JsonData.error("参数不足")
    return StreamingResponse(
        generate_stream_response(chat_service,account_id,request.message,course_id),
        media_type="text/event-stream"
    )

@router.post("/upload")
def upload_file(file:UploadFile,
                file_service:FileService=Depends(FileService),
               course_id:str=None,
                file_id:str=None,):
    try:
        file_embedding(file, file_service, file_id, course_id)
    except ApiException as e:
        return JsonData.error(e.msg)
    return JsonData.success("成功")

@router.post("/comment")
def comment(comments:str):
    result = comment_with_agent(comments)
    return JsonData.success(result)

@router.post("/analys")
def analys():
    path = create_analys_agent()
    return FileResponse(
        path=path,
        filename="ppt.pptx",
        media_type="application/octet-stream"
    )

